﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using BOMS.DBUtility;
using MySql.Data.MySqlClient;

namespace BOMS.DAL
{
    /// <summary>
    /// 数据访问类M_SubLans。
    /// </summary>
    public class M_SubLans
    {
        public M_SubLans()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            bool isExists = false;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from M_SubLans ");
            strSql.Append("where ID=@ID ");
            if (DbHelper.DBType == "MsSql")
            {
                SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
                parameters[0].Value = ID;

                isExists = DbHelperSQL.Exists(strSql.ToString(), parameters);
            }
            else if (DbHelper.DBType == "MySql")
            {
                MySqlParameter[] parameters = {
					new MySqlParameter("@ID", MySqlDbType.Int32,4)};
                parameters[0].Value = ID;

                isExists = DbHelperMySql.Exists(strSql.ToString(), parameters);
            }
            return isExists;
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public BOMS.Model.M_SubLans GetModel(int ID)
        {
            DataSet ds = new DataSet();
            BOMS.Model.M_SubLans model = new BOMS.Model.M_SubLans();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,Index,LanName,HostName,IP,Mac,Memo,RegTime,isDel from M_SubLans ");
            strSql.Append("where ID=@ID ");
            if (DbHelper.DBType == "MsSql")
            {
                SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
                parameters[0].Value = ID;
                 ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            }
            else if (DbHelper.DBType == "MySql")
            {
                MySqlParameter[] parameters = {
					new MySqlParameter("@ID", MySqlDbType.Int32,4)};
                parameters[0].Value = ID;
                ds = DbHelperMySql.Query(strSql.ToString(), parameters);
            }
            if (ds.Tables[0].Rows.Count > 0)
            {
                AssignModel(ds.Tables[0].Rows[0], model);
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public BOMS.Model.M_SubLans GetModelByEquipMac(string Mac)
        {
            DataSet ds = new DataSet();
            BOMS.Model.M_SubLans model = new BOMS.Model.M_SubLans();

            if (DbHelper.DBType == "MsSql")
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("select  top 1 ID,[Index],LanName,HostName,IP,Mac,Memo,RegTime,isDel from M_SubLans ");
                strSql.Append("where Mac=@Mac ");
                SqlParameter[] parameters = {
					new SqlParameter("@Mac", SqlDbType.NVarChar,30)};
                parameters[0].Value = Mac;

                 ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            }
            else if (DbHelper.DBType == "MySql")
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("select ID,`Index`,LanName,HostName,IP,Mac,Memo,RegTime,isDel from M_SubLans ");
                strSql.Append("where Mac=@Mac limit 1");
                MySqlParameter[] parameters = {
					new MySqlParameter("@Mac", MySqlDbType.VarChar,30)};
                parameters[0].Value = Mac;


                ds = DbHelperMySql.Query(strSql.ToString(), parameters);
            }
            if (ds.Tables[0].Rows.Count > 0)
            {
                AssignModel(ds.Tables[0].Rows[0], model);
                return model;
            }
            else
            {
                return null;
            }
        }
        public void AssignModel(DataRow dr, BOMS.Model.M_SubLans model)
        {
            if (dr["ID"].ToString() != "")
            {
                model.ID = int.Parse(dr["ID"].ToString());
            }
            model.Index = dr["Index"].ToString();
            model.LanName = dr["LanName"].ToString();
            model.HostName = dr["HostName"].ToString();
            model.IP = dr["IP"].ToString();
            model.Mac = dr["Mac"].ToString();
            model.Memo = dr["Memo"].ToString();
            if (dr["RegTime"].ToString() != "")
            {
                model.RegTime = (DbHelper.DBType == "MsSql" ? DateTime.Parse(dr["RegTime"].ToString())
                                                            : ((MySql.Data.Types.MySqlDateTime)dr["RegTime"]).GetDateTime());
                    
            }
            model.isDel = dr["isDel"].ToString();
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,Index,LanName,HostName,IP,Mac,Memo,RegTime,isDel ");
            strSql.Append("from M_SubLans ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelper.Query(strSql.ToString());
        }

       


        #endregion  成员方法
    }
}

